package controller;

import com.google.gson.Gson;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pojo.Blog;
import pojo.Collectiona;
import pojo.Follow;
import service.CollectionService;
import utils.util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

//快捷键Alt+Eenter,Alt+Insert
/*@Controller*/
@RestController//相当于controller+responsebody,下面的方法中返回值给安卓端就不用加 @ResponseBody
@RequestMapping("/collection")
public class CollectionController {
    @Resource
    private CollectionService collectionService;

    @RequestMapping("/addCollection")
    public String addCollection(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        String strCollection = map.get("strCollection");//获取安卓端传过来的键
        Gson gson = new Gson();
        Collectiona collectiona = gson.fromJson(strCollection, Collectiona.class);
        System.out.println("AAA" + collectiona.getUser().getUserid());
        collectiona.setTime(util.getNowTime());
        int i = collectionService.insert(collectiona);
        System.out.println("AAA" + i);
        if (i > 0) {
            return "succuse";
        } else {
            return "fail";     //保存失败
        }
    }



    @RequestMapping("/deletemycollection")
    public void deletemycollection(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        String strdelete = map.get("strdelete");//获取安卓端传过来的键
        Gson gson = new Gson();
        Collectiona collectiona=gson.fromJson(strdelete,Collectiona.class);
        int collectionid=collectiona.getCollectionid();
        collectionService.delete(collectionid);

    }
    @RequestMapping("/deleteCollection")
    public void deleteCollection(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        String coID = map.get("coID");//获取安卓端传过来的键
        int collectionid = Integer.parseInt(coID);
        System.out.println("coID" + collectionid);
        collectionService.delete(collectionid);

    }

    @RequestMapping("/getIsCollection")
    public void getIsCollection(@RequestParam Map<String, String> map, HttpServletResponse response) throws IOException {
        String userid = map.get("userid");
        String blogid = map.get("blogid");
        Collectiona collectiona = collectionService.getIsCollection(userid, blogid);
        response.setContentType("charset=UTF-8");
        System.out.println(collectiona);
        if (collectiona != null) {
            int collectionid = collectiona.getCollectionid();
            String cid = String.valueOf(collectionid);
            response.getWriter().write(cid);
            System.out.println(cid);
        } else {
            response.getWriter().write("collect");
        }
    }

    @RequestMapping("getCollectbyid")
    public void getCollectbyid(@RequestParam Map<String, String> map, HttpServletResponse response) {
        String strmycollect = map.get("strmycollect");
        Gson gson = new Gson();
        Collectiona collectiona = gson.fromJson(strmycollect, Collectiona.class);
        String userid = collectiona.getUser().getUserid();

        List<Collectiona> list = collectionService.getCollectbyid(userid);
        System.out.println(list);
        String JsonStr = new Gson().toJson(list);
        try {
            response.setContentType("charset=UTF-8");
            response.getWriter().write(JsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}